﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using TV_Organizer.Properties;
using System.ComponentModel;
using System.Collections.Specialized;

namespace TV_Organizer
{
    class PropertiesControlDataContext : DependencyObject
    {
        public PropertiesControlDataContext()
        {
            this.FileScannerTeams = new ObservableCollection<string>(Settings.Default.Teams.OfType<string>());
            this.FileScannerTeams.CollectionChanged += OnFileScannerTeams_CollectionChanged;
        }

        public static readonly DependencyProperty FileScannerTeamsProperty = DependencyProperty.Register("FileScannerTeams", typeof(ObservableCollection<string>), typeof(PropertiesControlDataContext));
        public ObservableCollection<string> FileScannerTeams
        {
            get { return (ObservableCollection<string>)GetValue(FileScannerTeamsProperty); }
            set { SetValue(FileScannerTeamsProperty, value); }
        }

        private void OnFileScannerTeams_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            foreach (var item in Settings.Default.Teams.OfType<string>().ToList())
            {
                if (!this.FileScannerTeams.Contains(item))
                    Settings.Default.Teams.Remove(item);
            }
            for (int i = 0; i < this.FileScannerTeams.Count; i++)
            {
                string item = this.FileScannerTeams[i];
                if (!Settings.Default.Teams.Contains(item))
                    Settings.Default.Teams.Insert(FileScannerTeams.IndexOf(item), item);
            }

            Settings.Default.Save();
        }
    }
}
